package api

import (
	"gitee.com/alex_li/beacon-config/internal/db"
	"gitee.com/alex_li/beacon-config/internal/db/model"
	"github.com/kataras/iris/v12"
	"github.com/lishimeng/app-starter"
	"github.com/lishimeng/go-log"
)

const (
	maxPageSize     = 100
	defaultPageSize = 10
	defaultPageNo   = 1
)

type DeviceInfoResp struct {
	DeviceId   int    `json:"deviceId,omitempty"`
	DeviceName string `json:"deviceName,omitempty"`
	Sn         string `json:"sn,omitempty"`
}
type DeviceInfoReq struct {
	DeviceId   int    `json:"deviceId,omitempty"`
	DeviceName string `json:"deviceName,omitempty"`
	Sn         string `json:"sn,omitempty"`
}

func Devices(ctx iris.Context) {

	log.Info("device list api")

	var err error
	var resp app.PagerResponse

	var pageNo = ctx.URLParamIntDefault("pageNum", 0)
	var PageSize = ctx.URLParamIntDefault("pageSize", 0)
	var name = ctx.URLParamDefault("name", "")

	if pageNo <= 0 {
		pageNo = defaultPageNo
	}
	if PageSize <= 0 {
		PageSize = defaultPageSize
	}
	if PageSize > maxPageSize {
		PageSize = maxPageSize
	}

	var devices []model.Device
	devices, err = db.GetDeviceList(pageNo, PageSize, name)
	if err != nil {
		resp.Code = 200
		return
	}

	for _, d := range devices {
		r := DeviceInfoResp{
			DeviceId:   d.DeviceID,
			DeviceName: d.DeviceName,
			Sn:         d.DeviceCode,
		}
		resp.Data = append(resp.Data, r)
	}

	resp.Code = 200
	_ = ctx.JSON(resp)
}

type EditDeviceReq struct {
	Id   int    `json:"id,omitempty"`
	Name string `json:"name,omitempty"`
}

func DeviceEditName(ctx iris.Context) {

	log.Info("update device name api")

	var err error
	var req EditDeviceReq
	var resp app.Response

	id := ctx.Params().GetIntDefault("id", 0)

	log.Info("id: %d", id)

	err = ctx.ReadJSON(&req)

	if err != nil {
		resp.Code = 500
		resp.Message = "req fail(edit dev_name)"
		_ = ctx.JSON(resp)
		return
	}

	// chk
	if id <= 0 {
		resp.Code = 500
		resp.Message = "unknown device"
		_ = ctx.JSON(resp)
		return
	}
	if len(req.Name) <= 0 {
		resp.Code = 500
		resp.Message = "device name empty"
		_ = ctx.JSON(resp)
		return
	}

	// update
	err = db.UpdateDeviceName(id, req.Name)
	if err != nil {
		resp.Code = 500
		resp.Message = "operation fail(edit dev_name)"
		_ = ctx.JSON(resp)
		return
	}

	resp.Code = 200
	_ = ctx.JSON(resp)
}

func addDeviceApi(ctx iris.Context) {
	var reqJson DeviceInfoReq
	var respJson app.Response
	respJson.Code = 200
	respJson.Message = "成功！"
	errJson := ctx.ReadJSON(&reqJson)

	if errJson != nil {
		log.Info("addDeviceApi [ctx.ReadJSON] err", errJson)
		respJson.Message = "添加失败！"
		respJson.Code = 500
		_ = ctx.JSON(respJson) //转为json格式
		return
	}
	device := model.Device{
		DeviceID:   reqJson.DeviceId,
		DeviceName: reqJson.DeviceName,
		DeviceCode: reqJson.Sn,
		TableInfo:  model.TableInfo{},
	}
	_, err := app.GetOrm().Context.Insert(&device)

	if err != nil {
		log.Info("addDeviceApi err", err)
		respJson.Message = "添加失败"
		respJson.Code = 500
		_ = ctx.JSON(respJson)
		return
	}
	_ = ctx.JSON(respJson)
}
func deleteDeviceApi(ctx iris.Context) {
	id := ctx.Params().GetIntDefault("id", 0)
	var respJson app.Response
	respJson.Code = 200
	respJson.Message = "成功！"

	if id == 0 {
		log.Info("deleteDeviceApi  err id=", id)
		respJson.Message = "删除失败！"
		respJson.Code = 500
		_ = ctx.JSON(respJson)
		return
	}
	_, err := app.GetOrm().Context.QueryTable(new(model.Device)).Filter("device_id", id).Delete()
	if err != nil {
		log.Info("deleteDeviceApi err", err)
		respJson.Message = "删除失败"
		respJson.Code = 500
		_ = ctx.JSON(respJson)
		return
	}
	_ = ctx.JSON(respJson)
}
